package Bean;

import java.io.UnsupportedEncodingException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;

public class ModifyUser extends Database {

	private String mCntruename = "";
	private String mEntruename = "";
	private Integer mSex = 0;
	private Integer mYear = 0;
	private Integer mMonth = 0;
	private Integer mDay = 0;
	private Integer mGyear = 0;
	private Integer mGmonth = 0;
	private Integer mEducation = 0;
	private String mStrEducation = "";
	private Integer mSchool1 = 0;
	private String mStrSchool1 = "";
	private Integer mSchool2 = 0;
	private String mStrSchool2 ="";
	private String mSchool = "";
	private String mEngraduateSchool = "";
	private Integer mDeparta = 0;
	private String mStrDeparta = "";
	private Integer mDepartb = 0;
	private String mStrDepartb = "";
	private String mCndepartment = "";
	private String mEndepartment = "";
	private Integer mBpa = 0;
	private Integer mBpb = 0;
	private Integer mPa = 0;
	private Integer mPb = 0;
	private String mStrBpa = "";
	private String mStrBpb = "";
	private String mStrPa = "";
	private String mStrPb = "";
	private String mEmail = "";
	private String mMobile = "";
	private String mTelphone = "";
	private String mCnaddress = "";
	private String mEnaddress = "";
	private String mZipcode = "";
	private String mHomepage = "";
	private boolean mIsSubmit = false;

	public String mError = "";

	public void setSubmit(String submit) {
		mIsSubmit = (submit != null && submit.equalsIgnoreCase("1"));
	}

	public void setCntruename(String str) {
		mCntruename = str;
	}
	
	public void setEntruename(String str) {
		mEntruename = str;
	}
	
	public void setSex(String str) {
		if (str != null && !str.trim().isEmpty())
			mSex = Integer.valueOf(str);
	}

	public void setYear(String str) {
		if (str != null && !str.trim().isEmpty())
			mYear = Integer.valueOf(str);
	}
	
	public void setMonth(String str) {
		if (str != null && !str.trim().isEmpty())
			mMonth = Integer.valueOf(str);
	}
	
	public void setGyear(String str) {
		if (str != null && !str.trim().isEmpty())
			mGyear = Integer.valueOf(str);
	}
	
	public void setGmonth(String str) {
		if (str != null && !str.trim().isEmpty())
			mGmonth = Integer.valueOf(str);
	}

	public void setDay(String str) {
		if (str != null && !str.trim().isEmpty())
			mDay = Integer.valueOf(str);
	}

	public void setEducation(String str) {
		if (str != null && !str.trim().isEmpty())
			mEducation = Integer.valueOf(str);
	}

	public void setSchool1(String str) {
		if (str != null && !str.trim().isEmpty())
			mSchool1 = Integer.valueOf(str);
	}

	public void setSchool2(String str) {
		if (str != null && !str.trim().isEmpty())
			mSchool2 = Integer.valueOf(str);
	}

	public void setSchool(String str) {
		mSchool = str;
	}

	public void setEngraduateSchool(String str) {
		mEngraduateSchool = str;
	}

	public void setDeparta(String str) {
		if (str != null && !str.trim().isEmpty())
			mDeparta = Integer.valueOf(str);
	}

	public void setDepartb(String str) {
		if (str != null && !str.trim().isEmpty())
			mDepartb = Integer.valueOf(str);
	}

	public void setCndepartment(String str) {
		mCndepartment = str;
	}

	public void setEndepartment(String str) {
		mEndepartment = str;
	}

	public void setBpa(String str) {
		if (str != null && !str.trim().isEmpty())
			mBpa = Integer.valueOf(str);
	}

	public void setBpb(String str) {
		if (str != null && !str.trim().isEmpty())
			mBpb = Integer.valueOf(str);
	}

	public void setPb(String str) {
		if (str != null && !str.trim().isEmpty())
			mPb = Integer.valueOf(str);
	}

	public void setPa(String str) {
		if (str != null && !str.trim().isEmpty())
			mPa = Integer.valueOf(str);
	}

	public void setEmail(String str) {
		mEmail = str;
	}

	public void setTelphone(String str) {
		mTelphone = str;
	}

	public void setMobile(String str) {
		mMobile = str;
	}

	public void setCnaddress(String str) {
		mCnaddress = str;
	}

	public void setEnaddress(String str) {
		mEnaddress = str;
	}

	public void setZipcode(String str) {
		mZipcode = str;
	}

	public void setHomepage(String str) {
		mHomepage = str;
	}

	public String getCntruename() {
		return mCntruename;
	}

	public String getEntruename() {
		return mEntruename;
	}

	public int getSex() {
		return mSex;
	}

	public int getYear() {
		return mYear;
	}
	
	public int getMonth() {
		return mMonth;
	}
	
	public int getGyear() {
		return mGyear;
	}
	
	public int getGmonth() {
		return mGmonth;
	}

	public int getDay() {
		return mDay ;
	}

	public String getStreducation() {
		return mStrEducation;
	}
	
	public int getEducation() {
		return mEducation;
	}

	public String getStrschool1() {
		return mStrSchool1;
	}

	public String getStrschool2() {
		return mStrSchool2;
	}
	
	public int getSchool1() {
		return mSchool1;
	}
	
	public int getSchool2() {
		return mSchool2;
	}

	public String getSchool() {
		return mSchool;
	}

	public String getEngraduateSchool() {
		return mEngraduateSchool;
	}

	public String getDeparta() {
		return mStrDeparta;
	}

	public String getDepartb() {
		return mStrDepartb;
	}

	public String getCndepartment() {
		return mCndepartment;
	}

	public String getEndepartment() {
		return mEndepartment;
	}

	public String getBpa() {
		return mStrBpa;
	}

	public String getBpb() {
		return mStrBpb;
	}

	public String getPb() {
		return mStrPb;
	}

	public String getPa() {
		return mStrPa;
	}

	public String getEmail() {
		return mEmail;
	}

	public String getTelphone() {
		return mTelphone;
	}

	public String getMobile() {
		return mMobile;
	}

	public String getCnaddress() {
		return mCnaddress;
	}

	public String getEnaddress() {
		return mEnaddress;
	}

	public String getZipcode() {
		return mZipcode;
	}

	public String getHomepage() {
		return mHomepage;
	}

	public boolean checkUser() {
		if (mCntruename.trim().isEmpty()) {
			printHtml("truename");
			return false;
		}

		if (mEntruename.trim().isEmpty()) {
			printHtml("entruename");
			return false;
		}

		if (mEmail.trim().isEmpty()) {
			printHtml("email");
			return false;
		}

		if (mSex < 1) {
			printHtml("sex");
			return false;
		}

		if (mYear < 1 || mMonth < 1 || mDay < 1) {
			printHtml("birth");
			return false;
		}

		if (mBpa < 1 || mBpb < 1) {
			printHtml("bornplace");
			return false;
		}

		if (mPa < 1 || mPb < 1) {
			printHtml("lifeplace");
			return false;
		}

		if (mGyear < 1 || mGmonth < 1) {
			printHtml("gyear");
			return false;
		}

		if (mEducation < 1) {
			printHtml("education");
			return false;
		}

		if ((mSchool1 < 1 || mSchool2 < 1) &&  mSchool.trim().isEmpty()) {
			printHtml("school");
			return false;
		}

		if ((mDeparta < 1 || mDepartb < 1) && mCndepartment.trim().isEmpty()) {
			printHtml("depart");
			return false;
		}

		if (mEngraduateSchool.trim().isEmpty()) {
			printHtml("enschool");
			return false;
		}

		if (mEndepartment.trim().isEmpty()) {
			printHtml("endepart");
			return false;
		}

		if (mMobile.trim().isEmpty() && mTelphone.trim().isEmpty()) {
			printHtml("contact");
			return false;
		}

		if (mCntruename.trim().isEmpty()) {
			printHtml("truename");
			return false;
		}

		return true;
	}

	private void printHtml(String error) {
		mError = "<html><body><script language=\"javascript\" type=\"text/javascript\">parent.valiglobal(\""
		       + error
		       + "\", true);</script></body></html>";
	}

	public void readUsr(int id) {
		String sql = "SELECT members.*, cate_education.list_name, cate_school1.list_name, cate_school2.list_name, cate_departa.list_name, "
				   + "cate_departb.list_name, cate_pa.list_name, cate_pb.list_name, cate_bpa.list_name, cate_bpb.list_name FROM members LEFT "
				   + "JOIN cate_education ON members.education = cate_education.id "
				   + "JOIN cate_school1 ON members.school1 = cate_school1.id "
				   + "JOIN cate_school2 ON members.school2 = cate_school2.id "
				   + "JOIN cate_departa ON members.departa = cate_departa.id "
				   + "JOIN cate_departb ON members.departb = cate_departb.id "
				   + "JOIN cate_pa ON members.pa = cate_pa.id "
				   + "JOIN cate_pb ON members.pb = cate_pb.id "
				   + "JOIN cate_bpa ON members.bpa = cate_bpa.id "
				   + "JOIN cate_bpb ON members.bpb = cate_bpb.id "
				   + "WHERE members.id = " + id;

		try {
			ResultSet rs = mStmt.executeQuery(sql);
			
			if (!rs.next()) {
				rs.close();
				return;
			}

			this.mCntruename = rs.getString("cntruename");
			this.mEntruename = rs.getString("entruename");
			this.mSex = rs.getInt("sex");
			this.mEducation = rs.getInt("education");
			this.mStrEducation = rs.getString("cate_education.list_name");
			this.mSchool = rs.getString("school");
			this.mStrSchool1 = rs.getString("cate_school1.list_name");
			this.mStrSchool2 = rs.getString("cate_school2.list_name");
			this.mEngraduateSchool = rs.getString("enschool");
			
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(rs.getDate("brith"));

			this.mYear = calendar.get(Calendar.YEAR);
			this.mMonth = calendar.get(Calendar.MONTH);
			this.mDay = calendar.get(Calendar.DAY_OF_MONTH);
			
			calendar.setTime(rs.getDate("edu_time"));
			
			this.mGyear = calendar.get(Calendar.YEAR);
			this.mGmonth = calendar.get(Calendar.MONTH);

			this.mStrDeparta = rs.getString("cate_departa.list_name");
			this.mStrDepartb = rs.getString("cate_departb.list_name");
			this.mCndepartment = rs.getString("cndepart");
			this.mEndepartment = rs.getString("endepart");
			this.mStrBpa = rs.getString("cate_bpa.list_name");
			this.mStrBpb = rs.getString("cate_bpb.list_name");
			this.mStrPa = rs.getString("cate_pb.list_name");
			this.mStrPb = rs.getString("cate_pb.list_name");
			this.mEmail = rs.getString("email");
			this.mTelphone = rs.getString("tel");
			this.mMobile = rs.getString("mobile");
			this.mCnaddress = rs.getString("cnaddress");
			this.mEnaddress = rs.getString("enaddress");
			this.mHomepage = rs.getString("homepage");
			this.mZipcode = rs.getString("zipcode");

			rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public int modify(int id) {
		if (!mIsSubmit) {
			return 1;
		}
		if (!checkUser()) {
			return 0;
		}

		String sql = null;
		try {
			sql = "UPDATE members SET "
					   + "cntruename = '" + (new String(mCntruename.getBytes("utf-8"), "utf-8"))
					   + "', entruename = '" + mEntruename
					   + "', sex = '" + --mSex
					   + "', brith = '" + mYear + "-" + mMonth + "-" + mDay
					   + "', edu_time = '" + mGyear + "-" + mGmonth + "-1"
					   + "', education = '" + mEducation
					   + "', school1 = '" + mSchool1
					   + "', school2 = '" + mSchool2
					   + "', school = '" + mSchool
					   + "', enSchool = '" + mEngraduateSchool
					   + "', departa = '" + mDeparta
					   + "', departb = '" + mDepartb
					   + "', cndepart = '" + mCndepartment
					   + "', endepart = '" + mEndepartment
					   + "', bpa = '" + mBpa
					   + "', bpb = '" + mBpb
					   + "', pa = '" + mPa
					   + "', pb = '" + mPb
					   + "', email = '" + mEmail
					   + "', mobile = '" + mMobile
					   + "', tel = '" + mTelphone
					   + "', cnaddress = '" + mCnaddress
					   + "', enaddress = '" + mEnaddress
					   + "', zipcode = '" + mZipcode
					   + "', homepage = '" + mHomepage
					   + "' WHERE id = " + id;
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		try {
			mStmt.executeUpdate(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return 2;
	}
}
